model_yes <- lm(action_yes~condition, data=new_jersey_data)
model_yes_robust <-coeftest(model_yes, vcov.=vcovHC(model_yes, type="HC1")) 

model_cluster <- lm(yes~condition+factor(match_id), data=national_data)
model_cluster_robust <- coeftest(model_cluster, vcov.=rddtools::vcovCluster(model_cluster, clusterVar=national_data$match_id), cluster = national_data$match_id) 

summarizer <- function(coef_test, study, treatments, model_terms){
	
	rows<-rep(study, length(treatments))
	cols<-c("study","treatment","coef","se","lb","ub")
	
	res<-as.data.frame(matrix(nrow=length(rows), ncol=length(cols)))
	colnames(res)<-cols
	res$treatment<-treatments
	res$study <- study
	
	for(i in 1:length(treatments)){
		res$coef[res$treatment==treatments[i]]<-coef_test[model_terms[i],"Estimate"]
		res$se[res$treatment==treatments[i]]<-coef_test[model_terms[i],"Std. Error"]
		
	}
	res$lb<-(res$coef-1.96*res$se)*100
	res$ub<-(res$coef+1.96*res$se)*100
	res$coef <- res$coef * 100
	return(res)
}

national_res <- summarizer(	coef_test = model_cluster_robust,
							study = "National",
							treatments = "Ranking Condition",
							model_terms = "condition2")

new_jersey_res <-summarizer(	coef_test = model_yes_robust,
							 study = "New Jersey",
							 treatments = c("Confidential Condition", "Ranking Condition", "Ranking and Anonymous Condition"),
							 model_terms = c("conditionConfidential", "conditionRanking", "conditionRanking + Anon"))

res <- rbind(national_res,new_jersey_res)

(
	main <- ggplot(res,aes(y=coef,x=treatment, shape=study, color=study))+
		geom_point(position = position_dodge(width=1), size=3)+
		geom_linerange(aes(ymin=lb,ymax=ub),position = position_dodge(width=1))+
		ylim(-20,20)+
		theme_bw()+
		ylab("Difference in agreement from control\n(percentage points)")+
		xlab("") + 
		geom_hline(yintercept=0, linetype="dotted") + 
		theme(panel.spacing = unit(1, "lines")) + 
		theme(panel.grid.major = element_line(colour = "white"),panel.grid.minor = element_line(colour = "white"), axis.title.x = element_text(vjust=-0.5)) +
		coord_flip()+
		theme(legend.position="right") +
		labs(col = "Study", shape="Study") #+ 
	#ggtitle("Affirmative Response to Collaboration Request")
)

ggsave(plot=main,"results/figure3.pdf",width=7, height=1.75)
